package com.cn.wanxi.publisher.thread;

import com.cn.wanxi.publisher.domain.entity.ThreadMsg;
import com.cn.wanxi.publisher.util.ParamUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.logging.Logger;

import static java.rmi.server.LogStream.log;

/**
 * @ClassName SelfThread
 * @Description TODO
 * @Author JiJiang
 * @Date 2022/9/28 23:04
 * @Version 1.0
 */
@Log4j2
public class SelfThread extends Thread {


    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    private int sendCount = 0;

    private RabbitTemplate rabbitTemplate;


    public SelfThread(String threadName, RabbitTemplate rabbitTemplate) {
        super(threadName);
        this.rabbitTemplate = rabbitTemplate;
    }


    @Override
    public void run() {

        //这里是为了让这个线程一直运行
        while (true) {
            if (rabbitTemplate == null) {
                System.out.println("线程\t" + getName() + "\tGameOver了");
                break;
            }

            //发送消息
            rabbitTemplate.convertAndSend(ParamUtil.EXCHANGE_NAME_THREAD
                    , "wx.thread.test"
                    //, new ThreadMsg(new Date(System.currentTimeMillis()), "线程：\t" + getName() + "\t发送的第" + (++sendCount) + "\t条信息"));
                    , new ThreadMsg(System.currentTimeMillis(), "线程：\t" + getName() + "\t发送的第" + (++sendCount) + "\t条信息"));

            log.info("");
            log.info("线程：\t" + getName() + "\t发送的第\t" + sendCount + "\t条消息");
            log.info("");

            //休眠1秒钟
            try {
                sleep(10);
            } catch (InterruptedException e) {
                System.out.println("线程\t" + getName() + "\tGameOver了");
                e.printStackTrace();
                break;
            }

        }


    }
}
